package com.ie.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.MyUtil;
import com.ie.common.http.ResponseResult;
import com.ie.entity.CartEntity;
import com.ie.entity.GoodsEntity;
import com.ie.entity.GoodsTypeEntity;
import com.ie.mapper.admin.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsEntity> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public ResponseResult<Map<String, Object>> getGoods(GoodsEntity goodsEntity) {
        if (goodsEntity.getCurrentPage() == null) {
            goodsEntity.setCurrentPage(1);
        }
        IPage<GoodsEntity> iPage = new Page<>(goodsEntity.getCurrentPage(), 2);
        //分页查询
        IPage<GoodsEntity> page = page(iPage);
        Map<String, Object> myres = new HashMap<>();
        myres.put("allGoods", page.getRecords());
        myres.put("totalPage", page.getPages());
        return ResponseResult.getSuccessResult(myres);
    }

    @Override
    public ResponseResult<Map<String, String>> add(GoodsEntity goodsEntity) {
        byte[] myfile = goodsEntity.getLogoFile();
        //如果选择了上传文件
        if (myfile != null && myfile.length > 0) {
            //这里的位置是前端Vue的静态资源路径，需要修改
//            String path = "D:\\idea-workspace\\ebusiness-vue\\src\\assets";
            String path = "E:\\Vue_Project\\ebusiness-vue\\src\\assets";
            //获得上传文件原名
            String fileName = goodsEntity.getFileName();
            //对文件重命名
            String fileNewName = MyUtil.getNewFileName(fileName);
            File filePath = new File(path + File.separator + fileNewName);
            //如果文件目录不存在，创建目录
            if (!filePath.getParentFile().exists()) {
                filePath.getParentFile().mkdirs();
            }
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(filePath);
                out.write(myfile);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            //将重命名后的图片名存到GoodsEntity对象中，添加到数据库时使用
            goodsEntity.setGpicture(fileNewName);
        }
        /**
         * 因添加修改的界面一样，使用GoodsEntity中的act来区分是添加还是修改
         */
        if ("add".equals(goodsEntity.getAct())) {
            boolean result = save(goodsEntity);
            if (result)//成功
                return ResponseResult.getMessageResult(null, "A001");
            else
                return ResponseResult.getMessageResult(null, "A002");
        } else {//修改
            boolean result = updateById(goodsEntity);
            if (result)
                return ResponseResult.getMessageResult(null, "A001");
            else
                return ResponseResult.getMessageResult(null, "A002");
        }
    }

    @Override
    public ResponseResult<Map<String, String>> delete(GoodsEntity goodsEntity) {
        //TODO: 2023/4/3 删除商品
        long res1 = 0;
        long res2 = 0;
        long res3 = 0;
        if (res1 > 0 || res2 > 0 || res3 > 0) {
            return ResponseResult.getMessageResult(null, "A001");
        }
        if (removeById(goodsEntity))
            return ResponseResult.getMessageResult(null, "A002");
        return ResponseResult.getMessageResult(null, "A003");

    }

    @Override
    public ResponseResult<List<GoodsEntity>> getAdvGoods() {
        List<GoodsEntity> listAdv = lambdaQuery().eq(GoodsEntity::getIsAdvertisement, 1)
                .orderByDesc(GoodsEntity::getId)
                .last("limit 5")
                .list();
        return ResponseResult.getSuccessResult(listAdv);
    }

    @Override
    public ResponseResult<List<GoodsEntity>> getGoodsIndex(GoodsEntity goodsEntity) {
        //构造条件
        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //如果goodstypeId不为空，则添加条件
        if (goodsEntity.getGoodstypeId() != null && goodsEntity.getGoodstypeId() != 0)
            lambdaQueryWrapper.eq(GoodsEntity::getGoodstypeId, goodsEntity.getGoodstypeId());
        //如果gname不为空，则添加条件
        if (goodsEntity.getGname() != null && goodsEntity.getGname().trim().length() > 0)
            lambdaQueryWrapper.like(GoodsEntity::getGname, goodsEntity.getGname());
        //查询结果按照GoodsEntity实体类中的id字段进行降序排列，并限制查询结果的数量为15条
        lambdaQueryWrapper.orderByDesc(GoodsEntity::getId).last("limit 15");
        //执行查询
        List<GoodsEntity> listIndex = list(lambdaQueryWrapper);
        return ResponseResult.getSuccessResult(listIndex);
    }

    @Override
    public ResponseResult<GoodsEntity> getGoodsById(GoodsEntity goodsEntity) {
        return ResponseResult.getSuccessResult(getById(goodsEntity.getId()));
    }

    @Override
    public ResponseResult<List<Map<String, Object>>> myCartGoods(CartEntity cartEntity) {

        //多表操作需要写xml
        List<Map<String, Object>> cartGoods = goodsMapper.myCartGoods(cartEntity.getBusertableId());
        return ResponseResult.getSuccessResult(cartGoods);
    }
}
